<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller'
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import updateForm from './updateForm.vue';
  import userlist from './userlist.vue';
  import whiteConfigList from './whiteConfigList.vue';
  import whiteUserList from './whiteUserList.vue';
  import { ElMessageBox } from 'element-plus';
  const updateFormRef = ref(),
  userlistRef = ref(),
  whiteConfigListRef = ref(),
  whiteUserListRef = ref()
  const action = reactive({
    gameList:[] as Array<any>,
    list:[] as Array<any>
  })
  const listQuery = reactive({
    game_id:'',
  })
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      game_id:'',
    })
    getList()
  }
  const openForm = (row:any = {})=>{
    updateFormRef.value.openDialog({...row,gameList:action.gameList})
  }
  const openUserList = (row:any = {})=>{
    whiteUserListRef.value.openDialog(row)
  }
  const openConfig = (row:any = {})=>{
    whiteConfigListRef.value.openDialog(row)
  }
  const handleDelete = (row:any = {})=>{
    const { id=0 } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm("确定删除此白名单吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      equipApi.whiteDelete({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getGameIdList = async()=>{
    const res = await equipApi.gameSwitch()
    const { data=[] } = res || {}
    action.gameList = data
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await equipApi.whiteList(listQuery)
    loading.close()
    const { data=[] } = res || {}
    action.list = data
  }
  onMounted(() => {
    getGameIdList()
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">游戏ID：</div>
          <el-select v-model="listQuery.game_id" clearable placeholder="请选择游戏ID">
            <el-option v-for="item in action.gameList" :label="item.game_name" :value="item.game_id" />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="openForm">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="120" prop="id" label="ID" />
      <el-table-column width="120" prop="game_id" label="游戏ID" />
      <el-table-column min-width="120" prop="gameName" label="游戏名称" />
      <el-table-column min-width="140" prop="content" label="描述" />
      <el-table-column min-width="140" prop="circulate" label="循环次数" />
      <el-table-column width="220" fixed=right label="操作">
        <template #default="scope">
          <el-row>
          <el-col class="xm-mb10" :span="12">
            <el-button v-rule="'equip_whiteUserList'" type="primary" @click="openUserList(scope.row)">白名单用户</el-button>
          </el-col>
          <el-col class="xm-mb10" :span="12">
            <el-button type="primary" @click="openForm(scope.row)">编辑</el-button>
          </el-col>
          <el-col v-rule="'equip_whiteConfig'" :span="12">
            <el-button type="primary" @click="openConfig(scope.row)">白名单配置</el-button>
          </el-col>
          <el-col :span="12">
            <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </el-col>
        </el-row>
          
        </template>
      </el-table-column>
    </el-table>
    <updateForm ref="updateFormRef" :gameList="action.gameList" @update-list="getList" />
    <userlist ref="userlistRef" />
    <whiteConfigList ref="whiteConfigListRef" />
    <whiteUserList ref="whiteUserListRef" />
  </div>
</template>

<style lang='scss' scoped>
  
</style>